package com.yubest.designpattern.behavior.mediator;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 聊天室
 *
 * @Author: hweiyu
 * @Date: 2022/10/27 11:51
 */
@Data
public class ChatRoomMediator {

    private List<ChatUser> chatUsers = new ArrayList<>();

    /**
     * 加入群聊
     * @param chatUser
     */
    public void join(ChatUser chatUser) {
        chatUsers.add(chatUser);
    }

    public void sendMsg(ChatUser chatUser, String msg) {
        System.out.println(String.format("%s说：%s", chatUser.getName(), msg));

        for (ChatUser user : chatUsers) {
            if (user != chatUser) {
                user.receiveMsg(msg);
            }
        }
    }
}
